package com.cn.demo4.dao.impl;

import com.cn.demo4.dao.HrDao;
import com.cn.demo4.model.HrModel;
import com.cn.demo4.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class HrDaoImpl implements HrDao {
    @Override
    public List<HrModel> findAll(HrModel hrModel) {
        StringBuilder sql = new StringBuilder();
        sql.append("select * from hr where true");
        if (!"".equals(hrModel.getTitle())) {
            sql.append(" and title like '%").append(hrModel.getTitle()).append("%'");
        }
        if (!"".equals(hrModel.getHref())) {
            sql.append(" and href like '%").append(hrModel.getHref()).append("%'");
        }
        if (!"".equals(hrModel.getContentOne())) {
            sql.append(" and content_one like '%").append(hrModel.getContentOne()).append("%'");
        }
        if (!"".equals(hrModel.getContentTwo())) {
            sql.append(" and content_two like '%").append(hrModel.getContentTwo()).append("%'");
        }
        if (hrModel.getStatus() != -1) {
            if (hrModel.getStatus() == 1) {
                sql.append(" and status=1");
            } else {
                sql.append(" and status is null and status=0");
            }
        }
        sql.append(" limit ").append((hrModel.getPageModel().getCurPage() - 1) * hrModel.getPageModel().getPageSize()).append(",").append(hrModel.getPageModel().getPageSize());
        ResultSet resultSet = JDBC.select(sql.toString());
        return getData(resultSet);
    }

    @Override
    public Integer add(HrModel hrModel) {
        String sql = "insert into hr(title,status,content_one,content_two,href,create_time,update_time,img" +
                "values('" + hrModel.getTitle() + "'," +
                "'" + hrModel.getStatus() + "','" + hrModel.getContentOne() + "'," +
                "'" + hrModel.getContentTwo() + "','" + hrModel.getHref() + "'," +
                " now(),now()),'" + hrModel.getImg() + "'";
        return JDBC.upDate(sql);
    }

    @Override
    public int del(Integer id) {
        String sql = "delete from hr where id=" + id;
        return JDBC.upDate(sql);
    }

    @Override
    public Integer update(HrModel hrModel) {
        String sql = "update hr set title='" + hrModel.getTitle() + "'," +
                "status=" + hrModel.getStatus() + "," +
                "content_one='" + hrModel.getContentOne() + "'," +
                "content_two='" + hrModel.getContentTwo() + "',href='" + hrModel.getHref() + "',";
        if (hrModel.getImg() != null && !hrModel.getImg().equals("undefined")) {
            sql += "img='" + hrModel.getImg() + "',";
        }
        sql += "update_time=now() where id=" + hrModel.getId();

        return JDBC.upDate(sql);
    }

    @Override
    public HrModel findById(Integer id) {
        String sql = "select * from hr where id=" + id;
        ResultSet resultSet = JDBC.select(sql);
        List<HrModel> list = getData(resultSet);
        return list.size() > 0 ? list.get(0) : null;
    }

    @Override
    public List<HrModel> getHrModelListForHome() {
        String sql = "select * from hr where status=1";
        ResultSet resultSet = JDBC.select(sql);
        return getData(resultSet);
    }

    @Override
    public int getCount(HrModel hrModel) {
        StringBuilder sql = new StringBuilder();
        sql.append("select count(*) count from hr where true");
        if (!"".equals(hrModel.getTitle())) {
            sql.append(" and title like '%").append(hrModel.getTitle()).append("%'");
        }
        if (!"".equals(hrModel.getHref())) {
            sql.append(" and href like '%").append(hrModel.getHref()).append("%'");
        }
        if (!"".equals(hrModel.getContentOne())) {
            sql.append(" and content_one like '%").append(hrModel.getContentOne()).append("%'");
        }
        if (!"".equals(hrModel.getContentTwo())) {
            sql.append(" and content_two like '%").append(hrModel.getContentTwo()).append("%'");
        }
        if (hrModel.getStatus() != -1) {
            if (hrModel.getStatus() == 1) {
                sql.append(" and status=1");
            } else {
                sql.append(" and status is null and status=0");
            }
        }
        ResultSet resultSet = JDBC.select(sql.toString());
        int count = 0;
        try {
            while (resultSet.next()) {
                count = resultSet.getInt("count");
            }
        } catch (SQLException e) {
//            e.printStackTrace();
        }
        return count;
    }

    private List<HrModel> getData(ResultSet resultSet) {
        List<HrModel> list = new ArrayList<>();
        try {
            while (resultSet.next()) {
                HrModel hrModel = new HrModel();
                hrModel.setId(resultSet.getInt("id"));
                hrModel.setHref(resultSet.getString("href"));
                hrModel.setImg(resultSet.getString("img"));
                hrModel.setTitle(resultSet.getString("title"));
                hrModel.setEmptyContent(resultSet.getString("empty_content"));
                hrModel.setContentOne(resultSet.getString("content_one"));
                hrModel.setContentTwo(resultSet.getString("content_two"));
                hrModel.setStatus(resultSet.getInt("status"));
                hrModel.setCreateTime(resultSet.getString("create_time"));
                hrModel.setUpdateTime(resultSet.getString("update_time"));
                list.add(hrModel);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}
